{UTF-8 без BOM}
Uses
	dos, routine, DispProc, Process, Buffer;
var
	d:TDispatcherProcess;
	T_read, T_write:longint;
	i, N:integer;
	log:text;
	B:TBuffer;
	numberMessage:byte;
	pathToFile:string;
	oldMemory, newMemory:	longint;
	main:descptr;
	threads:array[1..4] of body_process;
	numberProcess:integer;
procedure generator;
far;
var
	m:buf_message;
	hour,
	minute,
	sec,
	sec100:	word;
	shour,
	sminute,
	ssec,
	ssec100:string[5];
begin
	while true do
		begin
			{генерация сообщения}
			m:='';
			GetTime(hour, minute, sec, sec100);
			str(hour, shour); str(minute, sminute); str(sec, ssec);
			str(sec100, ssec100);
			m:='Message is created: ' + shour + ':' + sminute + ':'
			+ ssec + ':' + ssec100;
			{задержка}
			get_current_dispatcher^.delayTimeProcess(T_write);
			{отправка сообщения}
			GetTime(hour, minute, sec, sec100);
			str(hour, shour); str(minute, sminute); str(sec, ssec);
			str(sec100, ssec100);
			m:=m + '     Message is sent: ' + shour + ':' + sminute + ':'
			+ ssec + ':' + ssec100;
			B.write(m);
		end;
end;
procedure handler;
far;
var
	m:buf_message;
	hour,
	minute,
	sec,
	sec100:	word;
	shour,
	sminute,
	ssec,
	ssec100:string[5];
begin
	while true do
		begin
			{принятие сообщения}
			m:=B.read;
			GetTime(hour, minute, sec, sec100);
			str(hour, shour); str(minute, sminute); str(sec, ssec);
			str(sec100, ssec100);
			m:=m + '  Message is received: ' + shour + ':' + sminute + ':'
			+ ssec + ':' + ssec100;
			{задержка}
			get_current_dispatcher^.delayTimeProcess(T_Read);
			{обработка}
			GetTime(hour, minute, sec, sec100);
			str(hour, shour); str(minute, sminute); str(sec, ssec);
			str(sec100, ssec100);
			m:=m + '  Message was handled ' + shour + ':' + sminute + ':'
			+ ssec + ':' + ssec100;
			writeln(log, m);
		end;
end;
procedure normal;
far;
begin
	while true do
		begin
			writeln('running');
		end;
end;
procedure manager;
far;
begin
	while true do
		begin
			if (isEscape) then
				begin
					get_current_dispatcher^.stop;
				end;
		end;
end;

begin
	oldMemory:=MemAvail;
	new(main);
	writeln('Enter the path for log file:');
	readln(pathToFile);
	assign(log, pathToFile);
	rewrite(log);
	writeln('Enter the delay time of generated process');
	readln(T_Write);
	writeln('Enter the delay time of handler process');
	readln(T_Read);
	writeln('Enter the number of message in buffer:');
	readln(numberMessage);
	numberProcess:=4;
	threads[1]:=generator;
	threads[2]:=handler;
	threads[3]:=normal;
	threads[4]:=manager;
	d.init(numberProcess, threads, main);
	B.init(numberMessage);
	set_current_dispatcher(@d);
	newMemory:=memAvail;
	d.start;
	dispose(main);
	d.done;
	B.done;
	writeln('before the memory allocation: ', oldMemory);
	writeln('after the memory allocation: ', newMemory);
	writeln('after freeing memory: ', memAvail);
	close(log);

end.